#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

const int N = 100010;
const int M = 200010;

struct Edge
{
    int a;
    int b;
    int c;
    Edge(int x, int y, int z)
        :a(x)
        , b(y)
        , c(z)
    {}
};
struct cmp
{
    bool operator()(const Edge& x, const Edge& y)
    {
        return x.c < y.c;
    }
};
int n, m;
int p[N];
vector<Edge> edges;
bool st[N];

int find(int x)
{
    if (x != p[x]) p[x] = find(p[x]);
    return p[x];
}

int main()
{
    cin >> n >> m;

    while (m--)
    {
        int x, y, z; cin >> x >> y >> z;
        edges.emplace_back(x, y, z);
    }

    sort(edges.begin(), edges.end(), cmp());

    for (int i = 0; i <= n; ++i)
        p[i] = i;

    int res = 0, cnt = 0;
    for (auto e : edges)
    {
        // e.a, e.b, e.c
        int a = e.a, b = e.b, c = e.c;

        int pa = find(a), pb = find(b);
        if (pa != pb)
        {
            p[pa] = pb;
            res += c;
            cnt++;
        }
    }

    if (cnt < n - 1)
        puts("impossible");
    else
        cout << res << endl;
    return 0;
}